/**
 * Required imports.
 */
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

/**
* This class refills the fuel for the players ship, granted the player has enough funds.
*
* @author John J Storey
* @version 5
*/
public class FuelRefill extends JFrame {
	/**
	 * The model for the game.
	 */
	protected Model model;
	
	/**
	 * The main GUI for the game.
	 */
	protected View view;
	
	/**
	 * Spinner to choose how much to buy.
	 */
	protected JSpinner spinner;
	
	/**
	 * The frame for the new window.
	 */
	protected JFrame frame;
	
	/**
	 * Labels to be updated.
	 */
	protected JLabel lblYouCanBuy, lblPriceEach;
	
	/**
	*
	* this constructor sets the model and view of the panel
	* and lets the user know how much gas they can buy based off their funds.
	* adds the gas to the ship and subtracts the cost from the funds.
	*
	* @param model
	* @param view
	*/
	public FuelRefill(Model model, View view) {
		setSize(new Dimension(450, 185));
		setTitle("Buy Goods");
		getContentPane().setLayout(null);
		this.model = model;
		this.view = view;
		frame = this;

		spinner = new JSpinner();
		spinner.setModel(new SpinnerNumberModel(1, 1, 20 - model.getShip()
				.getFuel(), 1));
		((JSpinner.DefaultEditor) spinner.getEditor()).getTextField()
				.setEditable(false);
		spinner.setBounds(272, 56, 56, 20);
		spinner.addChangeListener(listener);
		getContentPane().add(spinner);

		final JLabel lblHowManyWould = new JLabel(
				"How many would you like to buy?");
		lblHowManyWould.setBounds(42, 59, 220, 14);
		getContentPane().add(lblHowManyWould);

		final JButton btnBuy = new JButton("Buy");
		btnBuy.setBounds(173, 110, 89, 23);
		btnBuy.addActionListener(buyActListener);
		getContentPane().add(btnBuy);

		lblYouCanBuy = new JLabel("You can buy up to "
				+ (20 - model.getShip().getFuel()));
		lblYouCanBuy.setBounds(42, 11, 135, 14);
		getContentPane().add(lblYouCanBuy);

		lblPriceEach = new JLabel("" + (Integer) spinner.getValue() * 2);
		lblPriceEach.setBounds(282, 11, 82, 14);
		getContentPane().add(lblPriceEach);

		final JLabel lblTotal = new JLabel("Total:");
		lblTotal.setBounds(243, 11, 35, 14);
		getContentPane().add(lblTotal);
	}
		
	/**
	*
	* Action listener that sets the new fuel level, funds, and possible
	* navigation distance.
	* Tells the user he or she has insufficient funds if they don't have
	* enough money.
	*
	*/
	private final ActionListener buyActListener = new ActionListener() {
		/**
		*
		* Action listener that sets the new fuel level, funds, and 
		* possible navigation distance.
		* Tells the user he or she has insufficient funds if they don't 
		* have enough money.
		*
		* @param arg0
		*/
		@Override
		public void actionPerformed(ActionEvent arg0) {
			if ((Integer) spinner.getValue() * 2 > model.getPlayer()
					.getCredits()) {
				JOptionPane.showMessageDialog(frame, "Insufficient Funds!");
			} else {
				model.getShip().setFuel(
						model.getShip().getFuel()
								+ (Integer) spinner.getValue());
				model.getPlayer().setCredit(
						model.getPlayer().getCredits()
								- (Integer) spinner.getValue() * 2);
				view.setFuel();
				view.setCurrency();
				view.drawNav();
				processWindowEvent(new WindowEvent(frame,
						WindowEvent.WINDOW_CLOSING));
			}
		}
	};
	
	/**
	*
	* Changes the listener based on the spinner value
	*
	*/
	private final ChangeListener listener = new ChangeListener() {

		@Override
		public void stateChanged(ChangeEvent arg0) {
			lblPriceEach.setText("" + (Integer) spinner.getValue() * 2);
		}
	};
}